package com.jiyinit.web.core.business.dao;

import com.jiyinit.core.framework.base.paging.PagingResult;
import com.jiyinit.core.framework.dao.mybatis.base.MybatisDaoContext;
import com.jiyinit.core.framework.dao.mybatis.paginator.domain.PageBounds;
import com.jiyinit.web.core.business.dto.DailyLogRequest;
import com.jiyinit.web.core.business.dto.TaskRequest;
import com.jiyinit.web.core.business.entity.*;
import com.jiyinit.web.core.business.enums.SystemConstant;
import com.jiyinit.web.core.business.vo.TaskInfoVo;
import com.jiyinit.web.core.common.util.UserUtil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p></p>
 * <p/>
 * <PRE>
 * <BR>	修改记录
 * <BR>-----------------------------------------------
 * <BR>	修改日期			修改人			修改内容
 * </PRE>
 *
 * @author wuyuefeng
 * @version 1.0
 * @date 2017/4/11 17:51
 * @since 1.0
 */
@Repository("core.tabCoreTaskDao")
public class TabCoreTaskDao {

    /**
     * 日志对象
     */
    private static Logger logger = LoggerFactory.getLogger(TabCoreTaskDao.class);

    private String SQLID = "core.tabCoreTaskDao.";

    @Autowired
    @Qualifier("core.MybatisDaoContext")
    private MybatisDaoContext mybatisDaoContext;

//    public List<TabCoreTaskMessage> findNewMessageList(List<TabCoreTask> list){
//        return mybatisDaoContext.findAll(SQLID+"findNewMessageList",TabCoreTaskMessage.class,list);
//    }

    public PagingResult<TaskInfoVo> receiveDailyLogList(DailyLogRequest request){
        PageBounds pageBounds = new PageBounds();
        pageBounds.setPage(request.getPage());
        pageBounds.setLimit(request.getLimit());
        HashMap<String,Object> params = new HashMap<>();
        params.put("userId",request.getUserId());
        params.put("ownName",request.getOwnName());
        params.put("startDate",request.getStartDate());
        params.put("endDate",request.getEndDate());

        return mybatisDaoContext.findForPage(SQLID + "receiveDailyLogList", TaskInfoVo.class, params,pageBounds);
    }

    public TabCoreTask getDailyLogByUserId(Integer userId){
        return mybatisDaoContext.findOne(SQLID+"getDailyLogByUserId",TabCoreTask.class,userId);
    }

    public List<TabCoreTask> findTaskByIds(List<TabSendMessageEntity> list){
        return mybatisDaoContext.findAll(SQLID+"findTaskByIds",TabCoreTask.class,list);
    }
   
	/*
	 * 条件查询
	 */
    public PagingResult<TaskInfoVo> findTasksByCons(TaskRequest taskRequest){
        PageBounds pageBounds = new PageBounds();
        pageBounds.setLimit(taskRequest.getLimit());
        pageBounds.setPage(taskRequest.getPage());
        return mybatisDaoContext.findForPage(SQLID + "findTasksByCons", TaskInfoVo.class, taskRequest.toMap(), pageBounds);
    }
    /**
     * 
         * 说明
         * @author wuyuefeng
         * @create 2017年5月11日 上午9:15:19
         * @param 
         * @return
     */
    public PagingResult<TaskInfoVo> findMyTasksByCons(TaskRequest taskRequest){
        PageBounds pageBounds = new PageBounds();
        pageBounds.setLimit(taskRequest.getLimit());
        pageBounds.setPage(taskRequest.getPage());
        return mybatisDaoContext.findForPage(SQLID + "findMyTasksByCons", TaskInfoVo.class, taskRequest.toMap(), pageBounds);
    }
    
    /**
     * 
         * 说明
         * @author wuyuefeng
         * @create 2017年5月11日 上午9:15:19
         * @param 
         * @return
     */
    public PagingResult<TaskInfoVo> findTasksAndAlloters(TaskRequest taskRequest){
        PageBounds pageBounds = new PageBounds();
        pageBounds.setLimit(taskRequest.getLimit());
        pageBounds.setPage(taskRequest.getPage());
        
        if(taskRequest.getWarnDays()!=null && taskRequest.getWarnDays()!=0){
        	Date d=new Date();
        	SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");   
        	//System.out.println("今天的日期："+df.format(d));   
        	//System.out.println("两天前的日期：" + df.format(new Date(d.getTime()+taskRequest.getWarnDays()*24*60*60*1000)));
        	
        	taskRequest.setEndTimePl(new Date(d.getTime() + taskRequest.getWarnDays()*24*60*60*1000));
        }
        taskRequest.setTaskCla(SystemConstant.SIMPLE_TASK);
        return mybatisDaoContext.findForPage(SQLID + "findTasksAndAlloters", TaskInfoVo.class, taskRequest.toMap(), pageBounds);
        
        
    }
    
    /**
     * 
         * 说明
         * @author wuyuefeng
         * @create 2017年4月25日 下午2:44:24
         * @param 
         * @return
     */
    public int addTask(TabCoreTask tabCoreTask) {
        return mybatisDaoContext.save(SQLID + "insert", tabCoreTask);
    }
    /**
     * 
         * 说明
         * @author wuyuefeng
         * @create 2017年4月25日 下午5:51:19
         * @param 
         * @return
     */
    public TabCoreTask findTaskById(Integer taskId){
        return mybatisDaoContext.findOne(SQLID + "selectByPrimaryKey", TabCoreTask.class, taskId);
    }
    /**
     * 
         * 说明
         * @author wuyuefeng
         * @create 2017年5月4日 下午3:20:29
         * @param 
         * @return
     */
    public List<TabCoreTask> findTaskByTitle(String title){
        return mybatisDaoContext.findAll(SQLID + "selectByTitle", TabCoreTask.class, title);
    }
   
    /**
     * 
         * 说明
         * @author wuyuefeng
         * @create 2017年4月26日 上午11:01:52
         * @param 
         * @return
     */
    public int updateTaskById(TabCoreTask tabCoreTask) {
        return mybatisDaoContext.update(SQLID + "updateByPrimaryKey", tabCoreTask);
    }
    public int update(TabCoreTask task){
        return mybatisDaoContext.update(SQLID +"update",task);
    }
    
    
    /**
     * 
         * 说明
         * @author wuyuefeng
         * @create 2017年4月26日 下午2:48:20
         * @param 
         * @return
     */
    
    public int deleteTaskByIds(Integer[] taskIds){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("taskIds", taskIds);
        return mybatisDaoContext.update(SQLID + "deleteTaskByIds", map);
    }
    /**
     * 
         * 说明
         * @author wuyuefeng
         * @create 2017年5月5日 下午2:26:55
         * @param 
         * @return
     */
    
    public List<TaskInfoVo> findChildsByid(Integer[] taskIds){
    	Map<String, Object> map = new HashMap<String, Object>();
        map.put("taskIds", taskIds);
    	return mybatisDaoContext.findAll(SQLID + "findChildsById", map);
    }
    
    /**
     * 
         * 说明
         * @author wuyuefeng
         * @create 2017年5月11日 上午9:15:19
         * @param 
         * @return
     */
    public PagingResult<TaskInfoVo> findAllMyTasks(TaskRequest taskRequest){
        PageBounds pageBounds = new PageBounds();
        pageBounds.setLimit(taskRequest.getLimit());
        pageBounds.setPage(taskRequest.getPage());
        
        if(taskRequest.getWarnDays()!=null && taskRequest.getWarnDays()!=0){
        	Date d=new Date();   
        	SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");   
        	//System.out.println("今天的日期："+df.format(d));   
        	//System.out.println("两天前的日期：" + df.format(new Date(d.getTime()+taskRequest.getWarnDays()*24*60*60*1000)));
        	
        	taskRequest.setEndTimePl(new Date(d.getTime() + taskRequest.getWarnDays()*24*60*60*1000));
        }
        
        
        return mybatisDaoContext.findForPage(SQLID + "findAllMyTasks", TaskInfoVo.class, taskRequest.toMap(), pageBounds);
    }

     /**
      * 根据我发布、我参与、待办、已办、临期和逾期等条件筛选任务数据
      * @author liuzongguang
      * @create 2018/10/23 17:38
      */
    public PagingResult<TaskInfoVo> findTasksByConditions(TaskRequest taskRequest){
        PageBounds pageBounds = new PageBounds();
        pageBounds.setLimit(taskRequest.getLimit());
        pageBounds.setPage(taskRequest.getPage());
        return mybatisDaoContext.findForPage(SQLID + "findTasksByConditions", TaskInfoVo.class, taskRequest.toMap(), pageBounds);
    }

    public TabCoreTask findTaskIdByTaskCla(List<Integer> list){
        return mybatisDaoContext.findOne(SQLID+"findOneTaskIdByTaskCla",TabCoreTask.class,list);
    }

    public List<TabCoreTask> findTaskIdByTaskCla(){
        return mybatisDaoContext.findAll(SQLID + "findTaskIdByTaskCla", TabCoreTask.class);
    }
}
